<template>
  <!-- 授权用户 -->
  <el-dialog title="选择用户" v-model="visible" class="dialog-lg" top="5vh" append-to-body @close="visible = false">
    <el-form :model="searchForm" ref="searchFormRef" inline>
      <el-form-item label="用户名称" prop="like.user_name">
        <el-input v-model="searchForm.like.user_name" placeholder="请输入用户名称" clearable class="w-150px" />
      </el-form-item>
      <el-form-item label="手机号码" prop="like.phone">
        <el-input v-model="searchForm.like.phone" placeholder="请输入手机号码" clearable class="w-150px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="search">搜索</el-button>
        <el-button icon="Refresh" @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <el-table @row-click="clickRow" ref="tableRef" :data="listData" @selection-change="handleSelection">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="用户名称" prop="user_name" show-overflow-tooltip />
        <el-table-column label="用户昵称" prop="nick_name" show-overflow-tooltip />
        <el-table-column label="邮箱" prop="email" show-overflow-tooltip />
        <el-table-column label="手机" prop="phone" show-overflow-tooltip />
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="create_time" width="150">
          <template #default="scope">
            <span>{{ parseTime(scope.row.create_time) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="searchForm.pageNum"
        v-model:limit="searchForm.pageSize"
        @pagination="getList" />
    </el-row>
    <template #footer>
      <div class="dialog-footer">
        <el-button
          class="medium"
          type="primary"
          :loading="submitting"
          :disabled="loading || !ids.length"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button class="medium" @click="visible = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="SelectUser">
  import { unAuthUsersListApi, authUserAppendApi } from '@/api/system/role'

  const props = defineProps({ roleId: [Number, String] })
  const visible = defineModel()
  const { proxy } = getCurrentInstance()

  const searchForm = reactive({
    pageNum: 1,
    pageSize: 10,
    roleId: undefined,
    like: { user_name: undefined, phone: undefined }
  })
  const { sys_normal_disable } = proxy.useDict('sys_normal_disable')

  // 初始化
  onMounted(() => {
    searchForm.roleId = props.roleId || undefined
    if (!searchForm.roleId) {
      proxy.$modal.msgError('参数错误')
      visible.value = false
      return
    }
    getList()
  })

  // 查询表数据
  const listData = ref([])
  const total = ref(0)
  const loading = ref(false)
  function getList() {
    loading.value = true
    unAuthUsersListApi(searchForm).then(res => {
      listData.value = res?.list || []
      total.value = res?.total || 0
      loading.value = false
    })
  }

  // 搜索和重置
  const searchFormRef = useTemplateRef('searchFormRef')
  function search() {
    searchForm.pageNum = 1
    getList()
  }
  function reset() {
    searchFormRef.value.resetFields()
    search()
  }

  // 选择行
  const tableRef = useTemplateRef('tableRef')
  function clickRow(row) {
    tableRef.value.toggleRowSelection(row)
  }

  // 多选框选中数据
  const ids = ref([])
  function handleSelection(selection) {
    ids.value = selection.map(item => item.id)
  }

  // 提交操作
  const emit = defineEmits(['submit'])
  const submitting = ref(false)
  function handleSubmit() {
    const roleId = searchForm.roleId
    const adminIds = ids.value.join(',')
    if (!adminIds) return proxy.$modal.msgError('请选择要分配的用户')
    if (submitting.value) return
    submitting.value = true

    authUserAppendApi(roleId, adminIds)
      .then(res => {
        emit('submit')
        proxy.$modal.msgSuccess(res.msg || '分配成功')
        visible.value = false
      })
      .catch(() => {
        submitting.value = false
      })
  }
</script>
